from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet
from apps.goods.models import SKU
from apps.meiduo_admin.serializers.sku import SkuModelSerializer, SkuSimpleSerializer
from apps.meiduo_admin.utils.pagination import MeiduoPagination
from django.db.models import Q

#sku表管理  获取sku表数据     #里面封装了增删改查等一系列的操作
class SkuViewSet(ModelViewSet):
    #SKu表的查询
    # queryset = SKU.objects.all()
    #过滤需要接受查询参数的信息，，而ViewSet封装的查询无法接收参数 故重写
    def get_queryset(self):
# request.query_params与Django标准的request.GET相同，只是更换了更正确的名称而已。
        keyword = self.request.query_params.get('keyword')
        #拼接查询语句
        queryset = SKU.objects
        if keyword:
            #标题或者副标题包含关键字
            queryset = queryset.filter(Q(name__contains=keyword) | Q(caption__contains=keyword))
        queryset = queryset.order_by('-id')
        return queryset

    serializer_class = SkuModelSerializer
    pagination_class = MeiduoPagination

class SkuSimpleView(ListAPIView):
    queryset = SKU.objects.all()
    serializer_class = SkuSimpleSerializer